/**
 *		Licensed to the Apache Software Foundation (ASF) under one
 *     or more contributor license agreements.  See the NOTICE file
 *     distributed with this work for additional information
 *     regarding copyright ownership.  The ASF licenses this file
 *     to you under the Apache License, Version 2.0 (the
 *     "License"); you may not use this file except in compliance
 *     with the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *      Unless required by applicable law or agreed to in writing,
 *     software distributed under the License is distributed on an
 *     "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *     KIND, either express or implied.  See the License for the
 *     specific language governing permissions and limitations
 *     under the License.
 *     
 *     Image Resizer Project
 */

package image.resizer;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Image resizing logic holder.
 * Responsible for scaling image to the defined bounds.
 * @author Ilya Dudarev
 */
public class ImageResizer {
	
	private static final String CLASS_NAME = ImageResizer.class.getName();
	private static final Logger logger = Logger.getLogger(ImageResizer.class.getPackage().getName());
	private static final Level FINER_LEVEL = Level.FINER;
	
	// Messages
	private static final String ILLEGAL_IMAGE_SIZE_BOUND = "Illegal image size upper bound supplied: {0}";

	/**
	 * Scales image to fit the bounds, wich defined by
	 * square with suppled edge "maxSize".
	 * @param toResize - image, need to feet the bounds.
	 * @param maxSize - maximal size ob image height/width.
	 * @return scaled image, if need, or original image, if
	 * supplied image size smaller/equals to supplied bounds. 
	 */
	public BufferedImage resizeIfBigger(BufferedImage toResize, int maxSize)
	{
		if(logger.isLoggable(FINER_LEVEL))
		{
			Object[] params = {"toResize ", toResize, "maxSize ", String.valueOf(maxSize)};
			logger.entering(CLASS_NAME, "resizeIfBigger", params);
		}
		if(maxSize <= 0)
		{
			IllegalArgumentException iae = new IllegalArgumentException(ILLEGAL_IMAGE_SIZE_BOUND);// TODO message formatter
			logger.throwing(CLASS_NAME, "resizeIfBigger", iae);
			throw iae;
		}
		int originalHeight = toResize.getHeight();
		int originalWidth = toResize.getWidth();
		int targetHeight = originalHeight;
		int targetWidth = originalWidth;
		// Finding bigger dimension need to be reduced
		if(originalHeight > originalWidth)
		{
			if(originalHeight > maxSize)
			{
				targetHeight = maxSize;
				targetWidth = (int)(targetHeight * (double)((double)originalWidth /(double)originalHeight));
			}
		}
		else
		{
			if(originalWidth > maxSize)
			{
				targetWidth = maxSize;
				targetHeight = (int)(targetWidth * (double)((double)originalHeight /(double)originalWidth));
			}
		}
        // Draw the scaled image
        BufferedImage resizedImage = new BufferedImage(targetWidth, 
          targetHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2D = resizedImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
          RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(toResize, 0, 0, targetWidth, targetHeight, null);
        if(logger.isLoggable(FINER_LEVEL))
        {
        	logger.exiting(CLASS_NAME, "resizeIfBigger", resizedImage);
        }
        return resizedImage;
	}
}
